<template>
  <div class="recommend" ref="recommend">
    <!-- 因为数据是异步获取的, scroll初始化的时候dom还没加载好, 这里传入渲染的数据, scroll监听改变并更新 -->
    <Scroll ref="scroll" class="recommend-content" :data="discList">
      <div>
        <recommend-swiper :banners="banners" />
        <div class="recommend-list">
          <h1 class="list-title">热门歌单推荐</h1>
          <ul>
            <li @click="selectItem(item)" class="item" v-for="item in discList" :key="item.id">
              <div class="icon">
                <img @load="loadImage" width="60" height="60" :src="item.picUrl" alt="">
              </div>
              <div class="text">
                <!-- v-html可以把获取的数据转义后再显示, 有点东西的 -->
                <h2 class="name" v-html="item.name"></h2>
                <p class="desc" v-html="item.copywriter"></p>
              </div>
            </li>
          </ul>
        </div>
      </div>
      <div class="loading-container" v-show="!discList.length">
        <loading />
      </div>
    </Scroll>
    <router-view></router-view>
  </div>
</template>

<script>
import RecommendSwiper from '@/base/slider/RecommendSwiper'
import Scroll from '@/base/scroll/Scroll'
import Loading from '@/base/loading/Loading'
import { getBanner, getPersonalized } from '@/api/index'
import { playlistMixin } from '../../assets/js/mixin'
import { mapMutations } from 'vuex'
// import { getRecommend, getDiscList } from '@/api/recommend.js'
// import { ERR_OK } from '@/api/config'
/*
为什么我们给scroll传数据只传了discList? 因为discList是较banners后面获取的, 所以discList获取完成后banners也就可以了.
BScroll就能正确计算高度了. 但是真实情况我们并不知道哪一块组件的图片先出来. 我们可以用一个计算属性计算一个新的数组,
只要这几个组件的图片有一个变化我们传的计算属性就变化并刷新refresh. 但是注意点: 我们获取到数据后就一定把高度撑开了吗?
因为我们的组件高度主要是靠图片撑开, 我们拿到数据还要通过src请求图片过来(还是一个异步过程)
解决方案: 我们在图片上监听一个@load="loadImage" 方法, 在这个方法里面调用refresh方法重新刷新
loadImage里面做一个限制, 只要有一张图片撑开高度就好, if (!this.checkLoaded) this.$refs.scroll.refresh()
*/

export default {
  name: 'Recommend',
  mixins: [playlistMixin],
  components: {
    RecommendSwiper,
    Scroll,
    Loading
  },
  data () {
    return {
      banners: [],
      discList: []
    }
  },
  methods: {
    /*
    _getRecommend() {
      getRecommend().then(res => {
        if (res.code === ERR_OK) {
          console.log(res)
          // this.recommends = res
        }
      })
    }
    */
    handlePlaylist(playlist) {
      const bottom = playlist.length > 0 ? '60px' : ''
      this.$refs.recommend.style.bottom = bottom
      this.$refs.scroll.refresh()
    },
    selectItem(item) {
      this.$router.push({
        path: `/recommend/${item.id}`
      })
      this.setDisc(item)
    },
    _getBanner() {
      getBanner().then(res => {
        this.banners = res.data.banners
      }).catch(err => {
        console.log(err)
      })
    },
    _getRecommend() {
      getPersonalized().then(res => {
        if (res.status === 200) {
          this.discList = res.data.result
        }
      }).catch(err => {
        console.log(err)
      })
    },
    loadImage() {
      if (!this.checkLoaded) {
        this.$refs.scroll.refresh()
        this.checkLoaded = true
      }
    },
    ...mapMutations({
      setDisc: 'SET_DISC'
    })
  },
  created () {
    this._getBanner()
    this._getRecommend()
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/css/variable.scss";
.recommend {
  position: fixed;
  width: 100%;
  top: 88px;
  bottom: 0;
  .recommend-content{
    height: 100%;
    overflow: hidden;
    .recommend-list{
      .list-title{
        height: 65px;
        line-height: 65px;
        text-align: center;
        font-size: $font-size-medium;
        color: $color-theme;
      }
      .item{
        display: flex;
        box-sizing: border-box;
        align-items: center;
        padding: 0 20px 20px 20px;
        .icon{
          flex: 0 0 60px;
          width: 60px;
          padding-right: 20px;
          img{
            border-radius: 5px;
            width: 60px;
            height: 60px;
          }
        }
        .text{
          display: flex;
          flex-direction: column;
          justify-content: center;
          flex: 1;
          line-height: 20px;
          overflow: hidden;
          font-size: $font-size-medium;
          .name{
            margin-bottom: 10px;
            color: $color-text;
          }
          .desc{
            color: $color-text-d;
          }
        }
      }
    }
    .loading-container{
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  }
}
</style>